<?php

namespace App\Http\Controllers\Blog;

use App\Http\Controllers\Controller;
use App\Http\Requests\Blog\BlogRequest;
use App\Models\Blog;
use App\Models\Category;
use Illuminate\Http\Request;
use Auth;


class BlogController extends Controller
{
    /**
     * 创建博客类型
     * @param BlogRequest $request
     * @return mixed
     */
    function create(BlogRequest $request)
    {
        $typeId = $request->get('type_id');
        $request->merge(['user_id' => Auth::guard('api')->user()->id]);
        if ($category = Category::where('id', $typeId)->first()) {
            if ($uuid = $request->get('uu_id')) {
                if ($blog = Blog::where('uu_id', $uuid)->first()) {
                    $blog->update($request->all());
                    return $this->success('更新成功', null);
                }
                return $this->error('为查询到数据', null);
            } else {
                Blog::create($request->all());
                $category->amount += 1;
                $category->save();
                return $this->success('创建成功', null);
            }


        }
        return $this->unValidate('未找到博客类型');
    }

    /**
     * 获取博客列表
     * @param Request $request
     * @return mixed
     */
    function list(Request $request)
    {
        $data = array();

        $hidden = $request->get('type', 'hidden');
        $hidden === 'hidden' && ($data['hidden'] = 0);
        $hidden === 'show' && ($data['hidden'] = 1);
        $type_id = $request->get('type_id');
        $type_id && ($data['type_id'] = $type_id);

        $query = Blog::where($data);
        if($request->get('admin') === '1') {
            $user = Auth::guard('api')->user();
            $query = $query->where('user_id', $user->id);
        }
        $blogList = $query->paginate(10);

        return $this->success('获取成功', $blogList, 10);
    }

    /**
     * 是否隐藏
     * @param Request $request
     * @return
     */
    function switch(Request $request)
    {
        $uuid = $request->get('uuid');
        if ($uuid) {
            if ($blog = Blog::where('uu_id', $uuid)->first()) {
                $blog->hidden = $blog->hidden ? 0 : 1;
                $blog->save();

                // 设置文章高度
                $typeId = Category::where('id', $blog->type_id)->first();
                if($blog->hidden) {
                    $typeId->amount -= 1;
                } else {
                    $typeId->amount += 1;
                }
                $typeId->save();

                return $this->success('修改成功', null);
            }
        }
        return $this->unValidate('未查询到该uuid');
    }

    /**
     * 获取详情
     * @param Request $request
     * @return mixed
     */
    function detail(Request $request)
    {
        $uuid = $request->get('uuid');
        if ($uuid) {
            if ($blog = Blog::with('user')->where('uu_id', $uuid)->first()) {
                $blog->view += 1;
                $blog->save();
                return $this->success('获取成功', $blog);
            }
        }
        return $this->unValidate('未查询到该uuid');
    }

    /**
     * 搜索博客
     * @param Request $request
     * @return string
     */
    public function search(Request $request)
    {
        if ($search = $request->get('search')) {
            $blog = Blog::where('hidden', '0')->where(function ($query) use ($search) {
                $query->where('title', 'like', "%$search%");
            })->get();
            return $this->success('获取成功', $blog);
        } else {
            return $this->unValidate('搜索词不能为空');
        }
    }

    /**
     * 点赞
     * @param Request $request
     * @return mixed
     */
    public function like(Request $request)
    {
        $uuid = $request->get('uuid');
        if ($uuid) {
            if ($blog = Blog::where('uu_id', $uuid)->first()) {
                $blog->like += 1;
                $blog->save();
                return $this->success('设置成功');
            }
        }
        return $this->unValidate('未查询到该uuid');
    }
}
